﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using Microsoft.Phone.Shell;

namespace depl
{
    public partial class ProgressDialog : UserControl
    {
        public ProgressTypes progressType;
        private string defaultText ="Downloading";
        internal Popup ChildWindowPopup { get; private set; }

        public enum ProgressTypes
        {
            IndeterminateMiddle,
            IndeterminateTop,
            DeterminateMiddle,
            DeterminateTop,
            CustomMiddle,
        }

        public ProgressDialog(string text)
        {
            InitializeComponent();
            defaultText = text;
        }

        public void Show()
        {
            if (this.ChildWindowPopup == null)
            {
                this.ChildWindowPopup = new Popup();

                try
                {
                    this.ChildWindowPopup.Child = this;
                }
                catch (ArgumentException)
                {
                    throw new InvalidOperationException("The control is already shown.");
                }
            }

            if (this.ChildWindowPopup != null && Application.Current.RootVisual != null)
            {
                // Configure accordingly to the type
                InitializeProgressType();
                // Show popup
                var content = Application.Current.Host.Content;
                LayoutRoot1.Width = content.ActualWidth;
                LayoutRoot1.Height = content.ActualHeight;
                //this.ChildWindowPopup.VerticalOffset = (content.ActualHeight - this.LayoutRoot.Height) / 2;
                //this.ChildWindowPopup.HorizontalOffset = (content.ActualWidth - this.LayoutRoot.Width) / 2;
                this.ChildWindowPopup.IsOpen = true;
            }

            
        }

        public void Hide()
        {
            this.ChildWindowPopup.IsOpen = false;
        }

        private void InitializeProgressType()
        {

            switch (this.progressType)
            {
                case ProgressTypes.IndeterminateMiddle:
                    this.Opacity = 1;
                    this.stackPanel.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    this.progressBar.Foreground = (Brush)Application.Current.Resources["PhoneAccentBrush"];
                    this.textBlockStatus.Text = defaultText;
                    this.textBlockStatus.Visibility = System.Windows.Visibility.Visible;
                    this.progressBar.IsIndeterminate = true;
                    break;
                case ProgressTypes.DeterminateMiddle:
                    this.Opacity = 1;
                    this.stackPanel.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    this.progressBar.Foreground = (Brush)Application.Current.Resources["PhoneAccentBrush"];
                    this.textBlockStatus.Text = defaultText;
                    this.textBlockStatus.Visibility = System.Windows.Visibility.Visible;
                    break;
                default:
                    break;
            }

        }
    }


}
